#pragma once

#include "RBTree.h"

namespace byh
{
	template<class K, class V>
	class map
	{
	public:
		struct KeyOfMap
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
		
		typedef typename RBTree<K, pair<const K, V>, KeyOfMap>::iterator iterator;
		typedef typename RBTree<K, pair<const K, V>, KeyOfMap>::const_iterator const_iterator;

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _t.insert(kv);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
		}

		iterator begin()
		{
			return _t.begin();
		}

		iterator end()
		{
			return _t.end();
		}

		const_iterator begin() const
		{
			return _t.begin();
		}

		const_iterator end() const
		{
			return _t.end();
		}

	protected:
		RBTree<K, pair<const K, V>, KeyOfMap> _t;
	};

	void Print(const map<int, int>& m)
	{
		map<int, int>::const_iterator it = m.begin();
		while (it != m.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
	}

	void Test_map()
	{
		map<int, int> m;
		m.insert({ 5,5 });
		m.insert({ 1,5 });
		m.insert({ 8,5 });
		m.insert({ 9,5 });
		m.insert({ 4,5 });
		m.insert({ 7,5 });
		m.insert({ 2,5 });
		Print(m);

		map<int, int> copy(m);
		Print(copy);

		//map<int, int> m1;
		//m1 = m;
		//Print(m1);

		vector<string> v = { "left", "right", "left", "right", "match" };
		map<string, int> tree;
		for (auto& e : v)
			tree[e]++;

		for (auto& e : tree)
			cout << e.first << ":" << e.second << endl;

		//map<int, int>::iterator it = m.begin();
		//it++;
		//it++;
		//it++;
		//it++;
		//it++;
		//while (it != m.begin())
		//{
		//	cout << it->first << ":" << it->second << endl;
		//	--it;
		//}
		//cout << endl;
	}
}